package com.shy.propagation;

import com.shy.propagation.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.springframework.transaction.annotation.Propagation.REQUIRED;
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;

@Service
public class OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private StockService stockService;

    @Transactional
    public void order() {
        orderDao.insert("下单");
        stockService.subStock();
        stockService.logStock();
        int s = 1 / 0;

    }

    public void delete() {
        orderDao.delete(11);
        orderDao.delete(12);
    }

    @Transactional(readOnly = true)
    public User query() {
        return orderDao.queryById(4);
    }

    @Transactional(readOnly = true)
    public void insert() {
        orderDao.insert("只读");
    }

    @Transactional(propagation = REQUIRES_NEW, timeout = 1)
    public User queryTimeout() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        return orderDao.queryById(4);
    }

}
